package upm.pb4agent.team5.common;


import jade.core.AID;
import jade.domain.FIPAException;
import jade.util.leap.ArrayList;
import jade.util.leap.HashMap;
import jade.util.leap.List;
import jade.util.leap.Map;
import upm.pb4agent.team5.BaseAgent;

public class Referee extends BaseAgent {
	private static final long serialVersionUID = 8740449938203386520L;
	public final static String DF_NAME = "Referee";
	
	private List subscribers = null;
	private Map positions = null;
	private int numOfTeamsCrossedFinishLine = 0;

    public Referee() {
		super(DF_NAME);
		this.subscribers = new ArrayList();
		this.positions = new HashMap();
	}
    
	/**
     * Method executed when agent is initialized
     */
	protected void setup() {
		System.out.println(getLocalName() + ": Enters the system");

		try {
			registerAgent();
			System.out.println(getLocalName() + ": Registered in DF");
			
			// Adding behaviours to manage messages
			addBehaviour(new BRefereeManageMessages(this));
		} catch (FIPAException e) {
			System.err.println(getLocalName()+": Cannot register in DF");
			doDelete(); // Terminating agent
		}
	}

	public void subscribe(AID aid) {
		getSubscribers().add(aid);
	}
	
	public void unsubscribe(AID aid) {
		getSubscribers().remove(aid);
	}

	public List getSubscribers() {
		return subscribers;
	}

	public Map getPositions() {
		return positions;
	}

	public int getNumOfTeamsCrossedFinishLine() {
		return numOfTeamsCrossedFinishLine;
	}

	public void incrementNumOfTeamsCrossedFinishLine() {
		this.numOfTeamsCrossedFinishLine++;
	}

}
